<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/1/22
 * Time: 12:49
 */
//$path =  $_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'weixin'.DIRECTORY_SEPARATOR.'access_token.json';
//// access_token 应该全局存储与更新，以下代码以写入到文件中做示例
//$data = json_decode(file_get_contents($path));
//var_dump($data);
//$url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx42c3bf4bc2151b67&secret=d177c0569b67f68024d1048481a0a052';
function https_request($url,$data = null){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt ( $curl, CURLOPT_SAFE_UPLOAD, false);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}
//var_dump(https_request($url));

$path =  $_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'weixin'.DIRECTORY_SEPARATOR;
if (!is_dir($path)) {
    @mkdir($path, 0777, 1);
}

$filename = $path . 1 . "_qr.jpg";
echo $filename;
getMId($filename);
function getMId($filename)
{
    $ACC_TOKEN = '6_J0Ksi5v_sHVH5Y0urSfHLtf6Imhzl_D9E_gotvf0ADmPkg6e9tJOZNOA1jY7KLeFmqj7HJEylHs9AfyQwiC_QGv6LhtBNjZDuBAoIdHFnXtrP9vXHd1wb49DPIiq-hnQrjqsBFgMRBg0vlszIYPdAGAMCT';
    $data = array(
        "media" => "@$filename"
    );
    $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=$ACC_TOKEN&type=image";

    $result = https_request($url, $data);
    var_dump($result);
//    $final = json_decode($result);
//    return $final->media_id;
}